﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Might.UIControls.PropertyEditor.Items {

    public partial class PositiveDoubleItem : UserControl , IPropertyEditorItemWithValue<double?> {
        
        private const string EMPTY_VALUE = "--";
            
        private double? value;
        private string label;
        private bool txtChanged = false;

        private string oldTxt = "";
        private int cursorPos = 0;
        private bool raiseEvents = true;                
        
        public PositiveDoubleItem( string label ) {
            InitializeComponent();
            this.label = label;
            labelTxt.Text = label + " : ";
        }
                             
        public event EventHandler ValueChanged;

        public double? Value {
            get {                 
                return value;                
            }
            set {
                this.value = value;               
                raiseEvents = true;
                if( value == null ) {
                    valueTxt.Text = EMPTY_VALUE;   
                } else {
                    valueTxt.Text = Math.Round( value.Value , 2 ).ToString(); //value.Value.ToString();
                }
                raiseEvents = false;
            }
        }        

        public string Label { get { return label; } }
        
        protected void onValueChanged( EventArgs e ) {
            if( ValueChanged != null ) {
                double result;
                if( Double.TryParse( valueTxt.Text , out result ) ) {
                    value = result;
                    ValueChanged( this , e );
                }                                                
            }
        }

        private void valueTxt_TextChanged( object sender , TextChangedEventArgs e ) {

            if( raiseEvents ) {
                return;
            }                        

            double result;
            if( ( double.TryParse( valueTxt.Text , out result ) && result >= 0 ) || valueTxt.Text == "" ) {
                oldTxt = valueTxt.Text;
                cursorPos = valueTxt.SelectionStart;
                txtChanged = true;
            } else {
                valueTxt.Text = oldTxt;
                valueTxt.SelectionStart = cursorPos;
            }
            
        }

        private void valueTxt_KeyDown( object sender , KeyEventArgs e ) {            
            if( txtChanged && e.Key == Key.Enter ) {
                txtChanged = false;
                onValueChanged( null );
            }
            cursorPos = valueTxt.SelectionStart;
        }

        private void valueTxt_GotFocus( object sender , RoutedEventArgs e ) {

            if( valueTxt.Text == EMPTY_VALUE ) {
                valueTxt.Text = "";
            }

            oldTxt = valueTxt.Text;
            cursorPos = valueTxt.SelectionStart;

            raiseEvents = false;

        }

        private void valueTxt_LostFocus( object sender , RoutedEventArgs e ) {
           
            raiseEvents = true;
            
            if( value == null && valueTxt.Text == EMPTY_VALUE ) {
                //nic, nemelo by nastat...pri got focus empty string zmizi
            } else if( value == null && valueTxt.Text == "" ) {
                valueTxt.Text = EMPTY_VALUE;
            } else if( value != null && valueTxt.Text == "" ) {                                
                valueTxt.Text = value.Value.ToString();
            } else {

                if( double.Parse( valueTxt.Text ) < 0 ) {
                    valueTxt.Text = "0";
                    txtChanged = true;
                }

                if( txtChanged ) {
                    txtChanged = false;
                    onValueChanged( null );
                }
                
            }                                                                                                               
            
        }        

        private void valueTxt_MouseLeftButtonDown( object sender , MouseButtonEventArgs e ) {
            cursorPos = valueTxt.SelectionStart;
        }

    }
    
}
